package com.gitee.wsl.time.interval

import kotlin.time.Duration.Companion.seconds

object Performance {
    private val interlMap = mutableMapOf<String,PerformanceEntry>()

    fun profileUpdates(name:String,delta: Long) {
        val updateProfiles = interlMap.getOrPut(name) { PerformanceEntry(name) }
        updateProfiles.profilePhysics(delta)
    }

    fun getUpdateProfile(name: String): PerformanceEntry? {
        return interlMap[name]
    }

}

class PerformanceEntry(val name: String){
    private val physicProfiles = ArrayDeque<Long>(60)
    fun profilePhysics(delta: Long) {
        physicProfiles.addFirst(delta)
        if (physicProfiles.size > 60) physicProfiles.removeLast()
    }

    val avgPhysicsTime: Long
        get() = 1.seconds.inWholeNanoseconds / (physicProfiles.sum() / physicProfiles.size)
}